package com.doris.media.picker.utils.permission;

import com.hjq.permissions.Permission;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public final class PermissionNameConvert {

    /**
     * 获取权限名称
     */
    public static String getPermissionString(List<String> permissions) {
        return listToString(permissionsToNames(permissions));
    }

    /**
     * String 列表拼接成一个字符串
     */
    private static String listToString(Set<String> hints) {
        if (hints == null || hints.isEmpty()) {
            return "相关权限说明";
        }

        StringBuilder builder = new StringBuilder();
        for (String text : hints) {
            if (builder.length() == 0) {
                builder.append(text);
            } else {
                builder.append("、").append(text);
            }
        }
        builder.append("权限说明");
        return builder.toString();
    }

    /**
     * 将权限列表转换成对应名称列表
     */
    private static Set<String> permissionsToNames(List<String> permissions) {
        Set<String> permissionNames = new HashSet<>();
        if (permissions == null) {
            return permissionNames;
        }
        for (String permission : permissions) {
            switch (permission) {
                case Permission.READ_EXTERNAL_STORAGE:
                case Permission.WRITE_EXTERNAL_STORAGE:
                    permissionNames.add("存储");
                    break;
                case Permission.MANAGE_EXTERNAL_STORAGE:
                    permissionNames.add("所有文件访问管理");
                    break;
                case Permission.READ_MEDIA_IMAGES:
                case Permission.READ_MEDIA_VIDEO:
                case Permission.READ_MEDIA_VISUAL_USER_SELECTED:
                    permissionNames.add("照片和视频");
                    break;
                case Permission.READ_MEDIA_AUDIO:
                    permissionNames.add("音乐和音频");
                    break;
                case Permission.CAMERA:
                    permissionNames.add("相机");
                    break;
                case Permission.RECORD_AUDIO:
                    permissionNames.add("麦克风");
                    break;
                case Permission.ACCESS_FINE_LOCATION:
                case Permission.ACCESS_COARSE_LOCATION:
                case Permission.ACCESS_BACKGROUND_LOCATION:
                    permissionNames.add("位置");
                    break;
                case Permission.BODY_SENSORS:
                case Permission.BODY_SENSORS_BACKGROUND:
                    permissionNames.add("身体传感器");
                    break;
                case Permission.BLUETOOTH_SCAN:
                case Permission.BLUETOOTH_CONNECT:
                case Permission.BLUETOOTH_ADVERTISE:
                    permissionNames.add("蓝牙");
                    break;
                case Permission.NEARBY_WIFI_DEVICES:
                    permissionNames.add("附近的设备");
                    break;
                case Permission.USE_SIP:
                case Permission.CALL_PHONE:
                case Permission.ADD_VOICEMAIL:
                case Permission.READ_PHONE_STATE:
                case Permission.READ_PHONE_NUMBERS:
                case Permission.ANSWER_PHONE_CALLS:
                    permissionNames.add("电话");
                    break;
                case Permission.GET_ACCOUNTS:
                case Permission.READ_CONTACTS:
                case Permission.WRITE_CONTACTS:
                    permissionNames.add("通讯录");
                    break;
                case Permission.READ_CALENDAR:
                case Permission.WRITE_CALENDAR:
                    permissionNames.add("日历");
                    break;
                case Permission.READ_CALL_LOG:
                case Permission.WRITE_CALL_LOG:
                    permissionNames.add("通话记录");
                    break;
                case Permission.ACTIVITY_RECOGNITION:
                    permissionNames.add("身体活动");
                    break;
                case Permission.ACCESS_MEDIA_LOCATION:
                    permissionNames.add("读取媒体文件位置");
                    break;
                case Permission.READ_SMS:
                case Permission.SEND_SMS:
                case Permission.RECEIVE_SMS:
                case Permission.RECEIVE_MMS:
                case Permission.RECEIVE_WAP_PUSH:
                    permissionNames.add("短信");
                    break;
                case Permission.REQUEST_INSTALL_PACKAGES:
                    permissionNames.add("安装应用");
                    break;
                case Permission.SYSTEM_ALERT_WINDOW:
                    permissionNames.add("悬浮窗");
                    break;
                case Permission.WRITE_SETTINGS:
                    permissionNames.add("修改系统设置");
                    break;
                case Permission.NOTIFICATION_SERVICE:
                    permissionNames.add("通知");
                    break;
                case Permission.POST_NOTIFICATIONS:
                    permissionNames.add("发送通知");
                    break;
                case Permission.BIND_NOTIFICATION_LISTENER_SERVICE:
                    permissionNames.add("通知栏监听");
                    break;
                case Permission.PACKAGE_USAGE_STATS:
                    permissionNames.add("查看应用使用情况");
                    break;
                case Permission.SCHEDULE_EXACT_ALARM:
                    permissionNames.add("查看闹钟提醒");
                    break;
                case Permission.ACCESS_NOTIFICATION_POLICY:
                    permissionNames.add("勿扰");
                    break;
                case Permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS:
                    permissionNames.add("忽略电池优化");
                    break;
                case Permission.BIND_VPN_SERVICE:
                    permissionNames.add("VPN");
                    break;
                case Permission.PICTURE_IN_PICTURE:
                    permissionNames.add("画中画");
                    break;
                case Permission.GET_INSTALLED_APPS:
                    permissionNames.add("读取应用列表");
                    break;
                default:
                    break;
            }
        }
        return permissionNames;
    }
}